package com.ruoyi.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.*;
import com.ruoyi.mapper.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.system.service.ISysUserService;

@RestController
@SaCheckLogin
@Api(tags = "医生")
@RequestMapping("/ray/doctor")
public class ApiDoctorController extends BaseController {

    @Autowired
    private FaDoctorApiMapper faDoctorApiMapper;

    @Autowired
    private ISysUserService userService;


    @Autowired
    private SysUser2Mapper sysUser2Mapper;

    @Autowired
    private SysUserRole2Mapper sysUserRole2Mapper;

    @Autowired
    private SysUserPost2Mapper sysUserPost2Mapper;

    @Autowired
    private SysDept2Mapper sysDept2Mapper;

//    @GetMapping("/list")
//    @ApiOperation("获取医生列表")
//    public TableDataInfo list(@RequestParam(name = "type",required = false,defaultValue = "") String type)
//    {
//        QueryWrapper<FaDoctorApi> wp = new QueryWrapper();
//
//        if(!type.isEmpty()){
//            System.out.println("type = " + type);
//            wp.eq("type",type);
//        }else {
////            wp.like("config_key","ray.");
//        }
//        wp.orderByDesc("weigh");
//
//        List<FaDoctorApi> list = faDoctorApiMapper.selectList(wp);
//        return getDataTable(list);
//    }

//    @PostMapping("/add")
//    @ApiOperation("添加医生")
//    public AjaxResult add(FaDoctorApi doctorApi)
//    {
//        QueryWrapper<FaDoctorApi> wp = new QueryWrapper();
//        wp.eq("name",doctorApi.getName());
//        FaDoctorApi res = faDoctorApiMapper.selectOne(wp);
//        if(res!=null){
//            return AjaxResult.error("已有相同名医生");
//        }
//
//        Integer insert = faDoctorApiMapper.insert(doctorApi);
//        if(insert>0)
//            return AjaxResult.success("添加成功",doctorApi);
//        return AjaxResult.error("添加识别");
//
//    }

//    @GetMapping("/list")
//    @ApiOperation("获取医生列表")
//    public TableDataInfo list(@RequestParam(name = "type",required = false,defaultValue = "") String type)
//    {
//        QueryWrapper<FaDoctorApi> wp = new QueryWrapper();
//
//        if(!type.isEmpty()){
//            System.out.println("type = " + type);
//            wp.eq("type",type);
//        }else {
////            wp.like("config_key","ray.");
//        }
//        wp.orderByDesc("weigh");
//
//        List<FaDoctorApi> list = faDoctorApiMapper.selectList(wp);
//        return getDataTable(list);
//    }


    @GetMapping("/get_dept_list")
    @ApiOperation("获取科室列表")
    public TableDataInfo get_dept_list() {
        startPage();
        QueryWrapper<SysDept2> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("user_name",username);

        List<SysDept2> list = this.sysDept2Mapper.selectList(queryWrapper);

        return getDataTable(list);
    }


    /**
     * 获取用户列表
     */

    @GetMapping("/list")
    @ApiOperation("获取医生列表")
    public TableDataInfo list(@RequestParam(name = "dept_id", required = false, defaultValue = "") Integer dept_id) {
        startPage();
//        List<SysUser> list = userService.selectUserList(user);

        QueryWrapper<SysUser2> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("user_name",username);
        if (dept_id != null) {
            queryWrapper.eq("dept_id", dept_id);
            System.out.println("dept_id = " + dept_id);
        }
        queryWrapper.orderByDesc("weigh");

        List<SysUser2> list_user = this.sysUser2Mapper.selectList(queryWrapper);
        List<SysUser3> list = new ArrayList<>();
        for (SysUser2 ut : list_user
        ) {
            SysUser3 u = new SysUser3();
            BeanUtil.copyProperties(ut, u);
            u.setSysUserPost(sysUserPost2Mapper.selectOne(new LambdaQueryWrapper<SysUserPost2>().eq(SysUserPost2::getUserId, u.getUserId())));
            if (u.getSysUserPost().getPostId() == 2L) {
                list.add(u);
            } else {
                System.out.println("u.getSysUserPost() = " + u.getSysUserPost());
            }

        }

        return getDataTable(list);
    }


    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @ApiOperation("添加医生")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody SysUser user) {
        Long[] a = {2L};
        user.setPostIds(a);
        user.setDeptId(2L);
        if (!userService.checkUserNameUnique(user)) {
            return error("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        } else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user)) {
            return error("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        } else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user)) {
            return error("新增用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }

        System.out.println("user = " + user);
//        user.setCreateBy(getUsername());
        user.setPassword(encryptPassword(user.getPassword()));
        return toAjax(userService.insertUser(user));
    }

    public static String encryptPassword(String password) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.encode(password);
    }



    @PostMapping("/update")
    @ApiOperation("更新医生")
    public AjaxResult update(SysUser2 user) {
        QueryWrapper<SysUser2> wp = new QueryWrapper();

        wp.eq("id", user.getUserId());
        SysUser2 res = sysUser2Mapper.selectOne(wp);
        if (res != null) {
            Integer update = sysUser2Mapper.updateById(user);
            if (update > 0)
                return AjaxResult.success("更新成功", user);
            return AjaxResult.error("更新失败");

        } else {
            return AjaxResult.error("没有该医生");
        }
    }

    @PostMapping("/del")
    @ApiOperation("删除医生")
    public AjaxResult del(Integer id) {
        Integer res = sysUser2Mapper.deleteById(id);
        if (res > 0) {
            return AjaxResult.success("删除成功", id);
        } else {
            return AjaxResult.error("没有该医生");
        }
    }



//    @PostMapping("/update")
//    @ApiOperation("更新医生")
//    public AjaxResult update(FaDoctorApi doctorApi) {
//        QueryWrapper<FaDoctorApi> wp = new QueryWrapper();
//        wp.eq("id", doctorApi.getId());
//        FaDoctorApi res = faDoctorApiMapper.selectOne(wp);
//        if (res != null) {
//            Integer update = faDoctorApiMapper.updateById(doctorApi);
//            if (update > 0)
//                return AjaxResult.success("更新成功", doctorApi);
//            return AjaxResult.error("更新失败");
//
//        } else {
//            return AjaxResult.error("没有该医生");
//        }
//    }


//    @PostMapping("/del")
//    @ApiOperation("删除医生")
//    public AjaxResult del(Integer id) {
//        Integer res = faDoctorApiMapper.deleteById(id);
//        if (res > 0) {
//            return AjaxResult.success("删除成功", id);
//        } else {
//            return AjaxResult.error("没有该医生");
//        }
//    }


}
